package com.eostoken.eos_wallet;

import android.app.Dialog;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.view.View;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;

import com.blankj.utilcode.constant.PermissionConstants;
import com.blankj.utilcode.util.AppUtils;
import com.blankj.utilcode.util.FragmentUtils;
import com.blankj.utilcode.util.GsonUtils;
import com.blankj.utilcode.util.ObjectUtils;
import com.blankj.utilcode.util.PermissionUtils;
import com.blankj.utilcode.util.StringUtils;
import com.blankj.utilcode.util.UtilsTransActivity;
import com.eostoken.eos_wallet.base.BaseActivity;
import com.eostoken.eos_wallet.bean.AppVersionBean;
import com.eostoken.eos_wallet.helper.DialogHelper;
import com.eostoken.eos_wallet.net.common.NetWorkManager;
import com.eostoken.eos_wallet.net.common.RxSubscriber;
import com.eostoken.eos_wallet.ui.activity.LauncherActivity;
import com.eostoken.eos_wallet.ui.fragment.AppsFragment;
import com.eostoken.eos_wallet.ui.fragment.HomeFragment;
import com.eostoken.eos_wallet.ui.fragment.MyFragment;
import com.eostoken.eos_wallet.utils.AppDataUtils;
import com.fredy.mvp.BasePresenter;
import com.fredy.mvp.BaseView;
import com.fredy.mvp.Model;
import com.google.gson.JsonObject;
import com.qmuiteam.qmui.util.QMUIDisplayHelper;
import com.qmuiteam.qmui.widget.tab.QMUITab;
import com.qmuiteam.qmui.widget.tab.QMUITabBuilder;
import com.qmuiteam.qmui.widget.tab.QMUITabSegment;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import io.reactivex.android.schedulers.AndroidSchedulers;

public class MainActivity extends BaseActivity {

    @BindView(R.id.tabSegment)
    QMUITabSegment mTabSegment;
    //    @BindView(R.id.contentViewPager)
//    QMUIViewPager mContentViewPager;
    @BindView(R.id.host_fragment)
    FrameLayout hostFragment;

    private List<Fragment> fragmentList = new ArrayList<>();
    private int curIndex = 0;


    @Override
    protected void initData() {
        getPermission();
        getVersion();
    }

    public void getPermission() {
        PermissionUtils.permission(PermissionConstants.STORAGE)
                .rationale(new PermissionUtils.OnRationaleListener() {
                    @Override
                    public void rationale(UtilsTransActivity activity, ShouldRequest shouldRequest) {
                        DialogHelper.showRationaleDialog(MainActivity.this, shouldRequest);
                    }
                })
                .callback(new PermissionUtils.FullCallback() {
                    @Override
                    public void onGranted(List<String> permissionsGranted) {

                    }

                    @Override
                    public void onDenied(List<String> permissionsDeniedForever, List<String> permissionsDenied) {

                    }
                }).request();
    }

    @Override
    public void initView() {
        initPagers();
    }

    @Override
    public int getContentView() {
        return R.layout.activity_main;
    }


    private void initPagers() {
        fragmentList.add(new HomeFragment());
        fragmentList.add(new AppsFragment());
        fragmentList.add(new MyFragment());
        FragmentUtils.add(
                getSupportFragmentManager(),
                fragmentList,
                R.id.host_fragment,
                new String[]{"HomeFragment", "AppFragment", "MyFragment"}, curIndex);
//        mContentViewPager.setAdapter(pagerAdapter);
//        mTabSegment.setupWithViewPager(mContentViewPager, false);
        QMUITabBuilder tabBuilder = mTabSegment.tabBuilder();
        tabBuilder.setSelectedIconScale(1.2f)
                .setTextSize(QMUIDisplayHelper.sp2px(this, 13), QMUIDisplayHelper.sp2px(this, 15))
                .setDynamicChangeIconColor(false);

        QMUITab component = tabBuilder
                .setNormalDrawable(ContextCompat.getDrawable(this, R.mipmap.wallet_normal_icon))
                .setSelectedDrawable(ContextCompat.getDrawable(this, R.mipmap.wallet_activated_icon))
                .setText(StringUtils.getString(R.string.str_wallet))
                .build(this);

        QMUITab lab = tabBuilder
                .setNormalDrawable(ContextCompat.getDrawable(this, R.mipmap.apps_normal_icon))
                .setSelectedDrawable(ContextCompat.getDrawable(this, R.mipmap.apps_activated_icon))
                .setText(StringUtils.getString(R.string.str_apps))
                .build(this);

        QMUITab util = tabBuilder
                .setNormalDrawable(ContextCompat.getDrawable(this, R.mipmap.my_normal_icon))
                .setSelectedDrawable(ContextCompat.getDrawable(this, R.mipmap.my_activated_icon))
                .setText(StringUtils.getString(R.string.str_my))
                .build(this);

        mTabSegment.addTab(component);
        mTabSegment.addTab(lab);
        mTabSegment.addTab(util);
//        mContentViewPager.setCurrentItem(0);
        mTabSegment.selectTab(curIndex);
        mTabSegment.addOnTabSelectedListener(new QMUITabSegment.OnTabSelectedListener() {
            @Override
            public void onTabSelected(int index) {
                showCurrentFragment(index);
//                mContentViewPager.setCurrentItem(index);
            }

            @Override
            public void onTabUnselected(int index) {
            }

            @Override
            public void onTabReselected(int index) {
            }

            @Override
            public void onDoubleTap(int index) {
                mTabSegment.clearSignCountView(index);
            }
        });
        mTabSegment.notifyDataChanged();
    }

    @Override
    public void onBackPressed() {
        if (!FragmentUtils.dispatchBackPress(fragmentList.get(curIndex))) {
            super.onBackPressed();
        }
    }

    private void showCurrentFragment(int index) {
        curIndex = index;
        FragmentUtils.showHide(index, fragmentList);
    }

    @Override
    public void onSaveInstanceState(@NonNull Bundle outState, @NonNull PersistableBundle outPersistentState) {
        super.onSaveInstanceState(outState, outPersistentState);
        outState.putInt("curIndex", curIndex);
    }

    @Override
    public Model createModel() {
        return null;
    }

    @Override
    public BaseView createView() {
        return null;
    }

    @Override
    public BasePresenter createPresenter() {
        return null;
    }

    @Override
    public void finishRefresh() {

    }

    @Override
    public boolean isShowState() {
        return true;
    }

    Dialog updateDialog;

    private void getVersion() {
        NetWorkManager.getRequest().getVersion("1", "1")
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new RxSubscriber<JsonObject>(MainActivity.this, false) {
                    @Override
                    public void onFail(String failMsg) {
                    }

                    @Override
                    public void onSuccess(JsonObject data) {
                        if (ObjectUtils.isNotEmpty(data)) {
                            AppVersionBean appVersionBean = GsonUtils.fromJson(data.toString(), AppVersionBean.class);
                            if (appVersionBean.getCode() > 0) {
                            } else {
                                AppVersionBean.DataBean dataBean = appVersionBean.getData();
                                if (dataBean != null) {
                                    String versioncode = dataBean.getVersioncode();
                                    if (ObjectUtils.isNotEmpty(versioncode))
                                        if (Integer.parseInt(versioncode) > AppUtils.getAppVersionCode()) {
                                            String url = dataBean.getDownload();
                                            String versiondes = dataBean.getVersiondes();
                                            updateDialog = DialogHelper.showUpdateDialog(MainActivity.this, versiondes, new View.OnClickListener() {
                                                @Override
                                                public void onClick(View v) {
                                                    if (updateDialog != null) {
                                                        updateDialog.dismiss();
                                                    }
                                                    AppDataUtils.downloadApk(MainActivity.this, url, "MangoWallet");
                                                }
                                            });
                                            return;
                                        }
                                }
                            }
                        }
                    }
                });
    }

}
